package com.solution.admin.product.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.solution.admin.product.entity.ProductUnitEntity;
import com.solution.admin.product.service.ProductUnitService;
import com.solution.common.constants.PublicStatus.STATUS;
import com.solution.common.constants.PublicStatus.SysCode;
import com.solution.common.controller.BaseController;
import com.solution.common.entity.BaseReturn;
import com.solution.common.page.JSONGrid;
import com.solution.common.page.PageBean;
import com.solution.common.page.PageParam;

/**
 * 计量单位配置web控制层
 * 
 * @author xu_cc
 * @date 2017-08-31 23:43:19
 */
@RequestMapping("/productUnit")
@Controller
public class ProductUnitController extends BaseController {

    private Logger logger = LoggerFactory.getLogger(ProductUnitController.class);

    @Autowired
    private ProductUnitService productUnitService;

    /**
     * 获取计量单位列表
     * 
     * @Title: getProductUnitList
     * @Description:(描述)
     * @param request
     * @return List<ProductUnitEntity>
     * @author xu_cc
     * @throws Exception
     *             exception
     * @Date 创建时间：2017年10月17日 下午3:46:44
     */
    @RequestMapping("/getUnitList")
    @ResponseBody
    public List<ProductUnitEntity> getProductUnitList(HttpServletRequest request) {
        try {
            // 计量单位
            List<ProductUnitEntity> productUnitList = productUnitService.listProductUnit();
            return productUnitList;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 跳转计量单位配置查询页面
     * 
     * @param ProductUnit
     *            接收界面查询条件参数
     * @param request
     *            请求对象
     * @return 返回对应的页面视图
     * @author xu_cc
     * @date 2017-08-31 23:43:19
     */
    @RequestMapping("/toListProductUnitPage")
    public String toListProductUnitPage(ProductUnitEntity productUnit, HttpServletRequest request) {
        return "product/productUnit_list";
    }

    /**
     * 分页查询计量单位配置列表
     * 
     * @param productUnit
     *            接收界面查询条件参数
     * @param request
     *            请求对象
     * @return 返回对应的页面视图
     * @author xu_cc
     * @date 2017-08-31 23:43:19
     */
    @RequestMapping("/listProductUnitPage")
    @ResponseBody
    public JSONGrid listProductUnitPage(ProductUnitEntity productUnit, HttpServletRequest request) {
        // int pageNum =
        // Integer.parseInt(request.getParameter("page").toString());
        // int numPerPage =
        // Integer.parseInt(request.getParameter("rows").toString());
        int pageNum = 1;
        int numPerPage = 10000;
        PageParam pageParam = super.getPageParam(request);
        pageParam.setPageNum(pageNum);
        pageParam.setNumPerPage(numPerPage);

        Map<String, Object> paramMap = new HashMap<String, Object>();
        try {
            PageBean pageBean = productUnitService.listProductUnitPage(pageParam, paramMap);
            return new JSONGrid(pageBean.getRecordList(), pageBean.getTotalCount());
        } catch (Exception e) {
            handleException("分页查询计量单位配置失败", logger, e);
        }
        return new JSONGrid();
    }

    /**
     * 跳转新增页面
     * 
     * @return string 跳转页面路径
     * @author xu_cc
     * @date 2017-08-31 23:43:19
     */
    @RequestMapping("/toAddProductUnit")
    public String toAddProductUnit(HttpServletRequest request) {
        request.setAttribute("opFlag", "add");
        return "product/productUnit_form";
    }

    /**
     * 跳转编辑页面
     * 
     * @return string 跳转页面路径
     * @author xu_cc
     * @date 2017-08-31 23:43:19
     */
    @RequestMapping("/toUpdateProductUnit")
    public String toUpdateProductUnit(HttpServletRequest request) {
        // 编辑操作
        request.setAttribute("opFlag", "edit");
        long id = Long.valueOf(request.getParameter("id"));
        try {
            ProductUnitEntity productUnit = productUnitService.getProductUnitById(id);
            request.setAttribute("productUnit", productUnit);
        } catch (Exception e) {
            handleException("跳转编辑计量单位配置页面错误", logger, e);
        }
        return "product/productUnit_form";
    }

    /**
     * 修改单位状态
     * 
     * @Title: modifyStatus
     * @Description:(描述)
     * @param request
     * @param tagId
     * @param status
     * @return BaseReturn
     * @author xu_cc
     * @throws Exception
     *             exception
     * @Date 创建时间：2017年10月16日 下午5:12:26
     */
    @RequestMapping("/modifyStatus")
    @ResponseBody
    public BaseReturn modifyStatus(HttpServletRequest request, Long id, Integer status) {
        BaseReturn result = new BaseReturn();
        try {
            ProductUnitEntity productUnit = new ProductUnitEntity();
            productUnit.setId(id);
            productUnit.setStatus(status);
            productUnit.setModifier(getUserName());
            productUnitService.updateProductUnit(productUnit);
            result.setCode(SysCode.SUCCESS);
            result.setMsg("单位状态修改成功！");
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(SysCode.ERROR);
            result.setMsg("单位状态修改异常！");
            return result;
        }
    }

    /**
     * 新增或保存计量单位配置
     * 
     * @param request
     *            请求对象
     * @param productUnitEntity
     *            计量单位配置实体
     * @return
     * @author xu_cc
     * @date 2017-08-31 23:43:19
     */
    @RequestMapping("/saveProductUnit")
    @ResponseBody
    public BaseReturn saveProductUnit(HttpServletRequest request, ProductUnitEntity productUnitEntity) {
        BaseReturn result = new BaseReturn();
        try {
            Long id = productUnitEntity.getId();
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("unitName", productUnitEntity.getUnitName());
            List<ProductUnitEntity> list = productUnitService.listProductUnit(map);
            if (null != id) {
                // 修改
                if (null != list && list.size() > 0) {
                    for (ProductUnitEntity unit : list) {
                        if (!id.equals(unit.getId())) {
                            result.setCode(SysCode.FAILED);
                            result.setMsg("已存在该单位！");
                            return result;
                        }
                    }
                }
                productUnitEntity.setModifier(getUserName());
                productUnitService.updateProductUnit(productUnitEntity);
            } else {
                // 新增
                if (null != list && list.size() > 0) {
                    result.setCode(SysCode.FAILED);
                    result.setMsg("已存在该单位！");
                    return result;
                }
                productUnitEntity.setStatus(STATUS.ENABLED.getId());
                productUnitEntity.setSortNo(1);
                productUnitEntity.setCreater(getUserName());
                productUnitEntity.setModifier(getUserName());
                productUnitService.saveProductUnit(productUnitEntity);
            }
            result.setCode(SysCode.SUCCESS);
            result.setMsg("单位信息保存成功！");
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(SysCode.ERROR);
            result.setMsg("单位信息保存异常！");
            return result;
        }
    }

}
